package com.ymatou.iapi.withdraw.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.xpath.internal.operations.And;
import com.ymatou.iapi.withdraw.parameter.QueryListBean;
import com.ymatou.iapi.withdraw.parameter.WithDrawInvoiceDto;
import com.ymatou.iapi.withdraw.parameter.WithDrawInvoiceDto2;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForBuyerBean;
import com.ymatou.iapi.withdraw.parameter.withDrawInvoiceListResp;
import com.ymatou.iapi.withdraw.parameter.withDrawInvoiceListResp2;
import com.ymatou.iapi.withdraw.service.QueryListCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForBuyerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.database.model.withdraw.Ymt_InvoiceApprovmentExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceFundFlowLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceOperationLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceSerialSeedExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawCounter;
import com.ymttest.database.model.withdraw.Ymt_WithDrawCounterExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawIdempotentExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoice;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceExExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceXloboLogExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceExample.Criteria;
import com.ymttest.database.sqlwapper.WithdrawWapper;
import com.ymttest.utils.TestDataManager;
/**
*提现记录列表--买家/买手提现记录查询
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryList{
	private static QueryListBean bean;
	private static QueryListCall call;
	
	Ymt_WithDrawInvoiceExample ymtinvoive=new Ymt_WithDrawInvoiceExample();//提现申请信息主表
	Ymt_InvoiceSerialSeedExample ymtserialseed=new Ymt_InvoiceSerialSeedExample();//单据编号种子表
	Ymt_WithDrawInvoiceXloboLogExample ymtxlobolog=new Ymt_WithDrawInvoiceXloboLogExample();//提现到贝海的日志
	Ymt_InvoiceFundFlowLogExample ymtflowlog=new Ymt_InvoiceFundFlowLogExample();//资金流转操作日志表
	Ymt_InvoiceOperationLogExample ymtoperationlog=new Ymt_InvoiceOperationLogExample();//单据操作日志表
	Ymt_WithDrawIdempotentExample ymtidempotent=new Ymt_WithDrawIdempotentExample();//提现申请幂等键记录表
	
	Ymt_WithDrawInvoiceExExample ymtinvoiveEx=new Ymt_WithDrawInvoiceExExample();//国内银行卡信息
	Ymt_InvoiceApprovmentExample ymtApprovement=new Ymt_InvoiceApprovmentExample();//单据审核记录表
	Ymt_WithDrawCounterExample ymtcounter=new Ymt_WithDrawCounterExample();//安利会提现计数
	
	applyBankWithDrawForBuyerBean bean2=new applyBankWithDrawForBuyerBean();
	applyBankWithDrawForBuyerCall call2=new applyBankWithDrawForBuyerCall();
	
	WithdrawWapper WithdrawWapper=new WithdrawWapper();
	
	
	private final int sellerid=3383;
	private final String sellerName="wesper";
	
	private final int userId=4085;
	private final String userName="lunchzhao";

	private final String fundAccountId_buyer="925CF663-FCBA-4380-846B-32162E58C317";
	private final String fundAccountName_buyer="吴文买家";
	private final String fundAccountNo_buyer="987654321789";
	
	private final String abroadFundAccountId="a0d8378e-af28-45bc-b211-5304d4869829";
	private final String abroadFundAccountName="wuwen";
	private final String abroadFundAccountNo="123456789123456";
	
	
	private final String fundAccountId_seller="925CF663-FCBA-4380-846B-32162E58C345";
	private final String fundAccountName_seller="吴文";
	private final String fundAccountNo_seller="123456789123";
	
	private final String alipayAccountNo_seller="18621614678";
	private final String alipayAccountName_seller="wuwenseller";
	private final String alipayAccountName_Id_seller="9d53eb3a-5867-4334-9735-269156eb7a26";
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("提现记录列表--买家/买手提现记录查询");
	}

	@Before
	public void caseUp() {
		bean=new QueryListBean();
		call=new QueryListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 买家提现到银行卡
	 * @return
	 * @throws Exception
	 */
	private String applyBanForBuyeranlihui() throws Exception {
		 //1:检查计数表Ymt_WithDrawCounter 一天一次是否已经计数
		//2:如果times=1 表示计数一次,不能再创建
		//3:修改计数times=0
		//4:删除当天的申请记录Ymt_WithDrawInvoice
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		LocalDateTime l=LocalDateTime.now();
		String begin=l.getYear()+"-"+l.getMonthValue()+"-"+l.getDayOfMonth()+" 00:00:00";
		ymtcounter.createCriteria().andCounterNameEqualTo("withdrawtimes").andTargetIdEqualTo("User"+userId+"_Type5")
		.andCycleTimeEqualTo(sf.parse(begin));
		
		List<Ymt_WithDrawCounter> counters=WithdrawWapper.selectYmtWithDrawCounter(ymtcounter);
		if(counters.size()==1)
		{
			Ymt_WithDrawCounter counter=counters.get(0);
			counter.setTimes(0);
			Map<String, Object> parameter=new HashMap<String, Object>();
			parameter.put("record", counter);
			parameter.put("example", ymtcounter);
			int updateCount=WithdrawWapper.updateCounterByExampleSelective(parameter);
			Logger.comment("### updateCount #### "+updateCount);
			
			ymtinvoive.createCriteria().andApplyTimeGreaterThanOrEqualTo(sf.parse(begin));
			int deletCount = WithdrawWapper.deleteByExample(ymtinvoive);
			Logger.comment("## deletCount # "+deletCount);
		}
		
		applyBankWithDrawForBuyerBean bean=new applyBankWithDrawForBuyerBean();
		applyBankWithDrawForBuyerCall call=new applyBankWithDrawForBuyerCall();
		//给帐号充值
		String accountid = AccountCallServiceV2.getAccountId(userId,5);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
				new BigDecimal(0));
		//冻结资金
		BigDecimal freeze= WithdrawWapper.getSumFreezebyUserId(userId);
		Logger.comment("################冻结资金："+freeze);
		//查看账户余额
		BigDecimal balance=AccountCallServiceV2.getAccountBalance(userId, 5);
		Logger.comment("################账户余额："+balance);
		
		bean.setUserId(userId);
		bean.setWithDrawChannel(1);
		bean.setAccountTypeId(5);//提现帐户类型Id，默认值:1.人民币余额帐户 5安利会
	    bean.setWithDrawAmount(new BigDecimal(10));
	    bean.setBusinessNo(new TestDataManager().getRandomNum(8));
        bean.setRemark("测试提现到国际银行卡");
        bean.setApplyUserId(userId);
        bean.setApplyLoginId(userName);
        
        bean.setBankAccountName(fundAccountName_buyer);
        bean.setBankAccountNo(fundAccountNo_buyer);
        bean.setFundAccountId(fundAccountId_buyer);
	    bean.setCurrencyType(1);//1 人名币
        bean.setIdempotentNo(new TestDataManager().getRandomNum(8));
        
		call.setData(bean);
		call.callService();	
		return  call.getInvoiceId();
    }
	/**
	 * 买家提现到银行卡
	 * @return
	 * @throws Exception
	 */
	private String applyBanForBuyer() throws Exception {
		applyBankWithDrawForBuyerBean bean=new applyBankWithDrawForBuyerBean();
		applyBankWithDrawForBuyerCall call=new applyBankWithDrawForBuyerCall();
		//给帐号充值
		String accountid = AccountCallServiceV2.getAccountId(userId, 1);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
				new BigDecimal(0));
		//冻结资金
		BigDecimal freeze= WithdrawWapper.getSumFreezebyUserId(userId);
		Logger.comment("################冻结资金："+freeze);
		//查看账户余额
		BigDecimal balance=AccountCallServiceV2.getAccountBalance(userId, 1);
		Logger.comment("################账户余额："+balance);
		
		bean.setUserId(userId);
		bean.setWithDrawChannel(1);
		bean.setAccountTypeId(1);//提现帐户类型Id，默认值:1.人民币余额帐户
	    bean.setWithDrawAmount(new BigDecimal(1000));
	    bean.setBusinessNo(new TestDataManager().getRandomNum(8));
        bean.setRemark("测试提现到国际银行卡");
        bean.setApplyUserId(userId);
        bean.setApplyLoginId(userName);
        
        bean.setBankAccountName(fundAccountName_buyer);
        bean.setBankAccountNo(fundAccountNo_buyer);
        bean.setFundAccountId(fundAccountId_buyer);
	    bean.setCurrencyType(1);//1 人名币
        bean.setIdempotentNo(new TestDataManager().getRandomNum(8));
        
		call.setData(bean);
		call.callService();	
		return  call.getInvoiceId();
    }
	
	 private void check_verify() throws InterruptedException {
		 //select top 10000 WithDrawType,ReceivableAccountType,ApproveStatus,InvoiceStatus,* from Ymt_WithDrawInvoice where 
		 //(UserId=4085  and AccountType=1 and WithDrawChannel<>10 and  InvoiceStatus<>-1) 
		 //or (UserId=4085  and AccountType=1 and WithDrawChannel<>10 and ApproveStatus<>0)
		 ymtinvoive=new Ymt_WithDrawInvoiceExample();
		 Criteria cinvoive= ymtinvoive.createCriteria();
		 Criteria cinvoive2= ymtinvoive.createCriteria();
			//1：根据条件查询申请表
			if(bean.getUserId()!=null)
			{
				cinvoive.andUserIdEqualTo(bean.getUserId());
				cinvoive2.andUserIdEqualTo(bean.getUserId());
			}
			if(bean.getApplyUserloginId()!=null)
			{
				cinvoive.andApplyUserLoginIdEqualTo(bean.getApplyUserloginId());
				cinvoive2.andApplyUserLoginIdEqualTo(bean.getApplyUserloginId());
			}
			if(bean.getAccountTypeId()!=null)
			{
				cinvoive.andAccountTypeEqualTo(bean.getAccountTypeId());
				cinvoive2.andAccountTypeEqualTo(bean.getAccountTypeId());
			}
			if(bean.getApproveStatusList()!=null && bean.getApproveStatusList().size()>0)//InvoiceStatus (-1.失效，0.待处理，1.处理中，2. 处理失败，3.已完成)
			{
				cinvoive.andApproveStatusIn(bean.getApproveStatusList());
				cinvoive2.andApproveStatusIn(bean.getApproveStatusList());
			}
			
			if(bean.getInvoiceStatusList()!=null && bean.getInvoiceStatusList().size()>0)
			{
				cinvoive.andInvoiceStatusIn(bean.getInvoiceStatusList());
				cinvoive2.andInvoiceStatusIn(bean.getInvoiceStatusList());
			}
			cinvoive.andWithDrawChannelNotEqualTo(10);//去掉推手，脏数据
			cinvoive2.andWithDrawChannelNotEqualTo(10);//去掉推手，脏数据
			
			// a and ( b or c ) = ( a and b) or ( a and c )
			cinvoive.andInvoiceStatusNotEqualTo(-1);
			
			//and  (InvoiceStatus<>-1 or ApproveStatus<>0)
			 cinvoive2.andApproveStatusNotEqualTo(0);
			 
			// ymtinvoive.or(cinvoive);
			 ymtinvoive.or(cinvoive2);
			 
			ymtinvoive.setOrderByClause("CreateTime desc");
			//ymtinvoive.getOrderByClause();
			
			List<Ymt_WithDrawInvoice> invoivelist = WithdrawWapper.selectYmtWithDrawInvoice(ymtinvoive);
			withDrawInvoiceListResp2 resp=call.getWithDrawInvoiceListResp();
			Logger.verifyEquals(resp.getTotal(),invoivelist.size(), "验证返回的total");
			check_withDrawInvoice(invoivelist,resp);
			
	}
	 
	  private void check_withDrawInvoice(List<Ymt_WithDrawInvoice> invoivelist,withDrawInvoiceListResp2 resp) {
		  
		  List<WithDrawInvoiceDto2> resps=resp.getWithDrawInvoiceList();
		  for(int i=0;i<invoivelist.size();i++){
			  WithDrawInvoiceDto2 exp=resps.get(0);
			  Ymt_WithDrawInvoice act=invoivelist.get(0);
//			  Logger.comment("##WithDrawInvoiceId exp:##  "+exp.getWithDrawInvoiceId());
//			  Logger.comment("##WithDrawInvoiceId act:##  "+act.getWithDrawInvoiceId());
		     Logger.verifyEquals(exp.getAccountType(), act.getAccountType(), "验证第"+(i+1)+"条 AccountType");
		     Logger.verifyEquals(sdf.format(exp.getApplyTime()), sdf.format(act.getApplyTime()), "验证第"+(i+1)+"条 ApplyTime");
		     Logger.verifyEquals(exp.getApplyUserLoginId(), act.getApplyUserLoginId(), "验证第"+(i+1)+"条 ApplyUserLoginId");
		     //ApprovalResult
		    // Logger.verifyEquals(exp.getApprovalResult(), act.geta(), "验证第"+(i+1)+"条 AccountType");
		     Logger.verifyEquals(exp.getApproveStatus(), act.getApproveStatus(), "验证第"+(i+1)+"条 ApproveStatus");
		     Logger.verifyEquals(exp.getBankAccountName(), act.getBankAccountName(), "验证第"+(i+1)+"条 BankAccountName");
		     Logger.verifyEquals(exp.getBankAccountNo(), act.getBankAccountNo(), "验证第"+(i+1)+"条 BankAccountNo");
		     //BankBranchName
		     //IsCorporate
		    // Logger.verifyEquals(exp.get(), act.getApproveStatus(), "验证第"+(i+1)+"条 ApproveStatus");
		     Logger.verifyEquals(exp.getBankCity(), act.getBankCity(), "验证第"+(i+1)+"条 BankCity");
		     Logger.verifyEquals(exp.getBankCountry(), act.getBankCountry(), "验证第"+(i+1)+"条 BankCountry");
		     Logger.verifyEquals(exp.getBankHostCity(), act.getBankHostCity(), "验证第"+(i+1)+"条 BankHostCity");
		     Logger.verifyEquals(exp.getBankName(), act.getBankName(), "验证第"+(i+1)+"条 BankName");
		     Logger.verifyEquals(exp.getBankState(), act.getBankState(), "验证第"+(i+1)+"条 BankState");
		     //BatchCode
		     Logger.verifyEquals(exp.getBranchAddress(), act.getBranchAddress(), "验证第"+(i+1)+"条 BranchAddress");
		     Logger.verifyEquals(exp.getBusinessName(), act.getBusinessName(), "验证第"+(i+1)+"条 BusinessName");
		     Logger.verifyEquals(exp.getBranchAddress(), act.getBranchAddress(), "验证第"+(i+1)+"条 BranchAddress");
		     Logger.verifyEquals(exp.getBusinessName(), act.getBusinessName(), "验证第"+(i+1)+"条 BusinessName");
		     //CurrencyRate
		   //  Logger.verifyEquals(exp.getCurrencyRate(), act.getCurrencyType(), "验证第"+(i+1)+"条 CurrencyRate");
		     //Email
		   //  Logger.verifyEquals(exp.getEmail(), act.g(), "验证第"+(i+1)+"条 BranchAddress");
		     //InputResultInfo
		     Logger.verifyEquals(exp.getInvoiceStatus(), act.getInvoiceStatus(), "验证第"+(i+1)+"条 InvoiceStatus");
		     Logger.verifyEquals(exp.getPoundageAmount().stripTrailingZeros().toPlainString(), act.getPoundageAmount().stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条 PoundageAmount");
		     Logger.verifyEquals(exp.getReceivableAccountType(), act.getReceivableAccountType(), "验证第"+(i+1)+"条 ReceivableAccountType");
		     Logger.verifyEquals(exp.getReceiverAddress(), act.getReceiverAddress(), "验证第"+(i+1)+"条 ReceiverAddress");
		     Logger.verifyEquals(exp.getRemark(), act.getRemark(), "验证第"+(i+1)+"条 Remark");
		     Logger.verifyEquals(exp.getRoutingNumber(), act.getRoutingNumber(), "验证第"+(i+1)+"条 RoutingNumber");
		     Logger.verifyEquals(exp.getSwiftCode(), act.getSwiftCode(), "验证第"+(i+1)+"条 SwiftCode");
		     Logger.verifyEquals(exp.getThirdPartyPlatformAccount(), act.getThirdPartyPlatformAccount(), "验证第"+(i+1)+"条 ThirdPartyPlatformAccount");
		     Logger.verifyEquals(sdf.format(exp.getUpdatTime()), sdf.format(act.getUpdateTime()), "验证第"+(i+1)+"条 UpdateTime");
		     Logger.verifyEquals(exp.getUserAccountType(), act.getUserAccountType(), "验证第"+(i+1)+"条 UserAccountType");
		     Logger.verifyEquals(exp.getUserid(), act.getUserId(), "验证第"+(i+1)+"条 Userid");
		     Logger.verifyEquals(exp.getUserLoginId(), act.getUserLoginId(), "验证第"+(i+1)+"条 UserLoginId");
		     Logger.verifyEquals(exp.getWithDrawAmount().stripTrailingZeros().toPlainString(), act.getWithDrawAmount().stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条 WithDrawAmount");
		     Logger.verifyEquals(exp.getWithDrawInvoiceId(), act.getWithDrawInvoiceId(), "验证第"+(i+1)+"条 WithDrawInvoiceId");
		     Logger.verifyEquals(exp.getWithDrawType(), act.getWithDrawType(), "验证第"+(i+1)+"条 WithDrawType");
		     Logger.verifyEquals(exp.getIBanCode(), act.getIBanCode(), "验证第"+(i+1)+"条 IBanCode");
		  }
		  
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryList_001() {
		Logger.start(true,"正例-提现记录列表--买家/买手提现记录查询-安利会用户");
		try {
			
			String invoiceId=applyBanForBuyeranlihui();
			bean.setUserId(userId);
			bean.setAccountTypeId(5);
			call.setData(bean);
			call.callService();
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryList_002() {
		Logger.start(true,"正例-提现记录列表--买家/买手提现记录查询-普通用户");
		try {
			
			//String invoiceId=applyBanForBuyer();
			
			bean.setUserId(userId);
			bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}